/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.questionnaire;

import org.ipti.br.tag.bean.AppBaseBean;
import org.ipti.br.tag.bean.SessionBean;
import org.ipti.br.tag.bean.dataManipulation.HumanHbMeasure;
import org.ipti.br.tag.bo.CampaignBO;
import org.ipti.br.tag.bo.HbBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import org.ipti.br.tag.entity.enums.CampaignType;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.ipti.br.tag.pdf.AnamnesePDF;
import org.ipti.br.tag.pdf.PrescriptionPDF;

/**
 *
 * @author FelipeMSX
 */
@Named @ViewScoped
public class HbBean extends AppBaseBean implements Serializable {

    @Inject private SessionBean sessionBean;
    @Inject private UnityBO unityBO;
    @Inject private CampaignBO campaignBO;
    @Inject private HbBO hbBO;
    @Inject private AnamnesePDF anemnesePDF;
    @Inject private PrescriptionPDF prescriptionPDF;
    
    private Unity unity;
    private Campaign campaign;
    private List<HumanHbMeasure> humanHbMeasures;
    private Integer concentration;
    private Date consultationDate;
    private int year = getActualYear();

    @PostConstruct
    public void init() {
        this.humanHbMeasures = new ArrayList<>();
        this.campaign = null;
        this.unity = null;
    }

    public List<Unity> getUnityList() {
        if (this.campaign != null) {
            return unityBO.getClassLessonList(sessionBean.getLoggedActor().getUnity(), year);
        } else {
            return null;
        }
    }

    public List<Campaign> getCampaignList() {
        return campaignBO.findAllCampaign(CampaignType.ANEMIA_FERROPRIVA);
    }

    public void loadDataTable() {
        if (campaign != null && unity != null) {
            this.humanHbMeasures = hbBO.getDataTableHB(unity, campaign);
        }
    }

    public void save() {
        try {
            hbBO.saveHB(humanHbMeasures, campaign);
            addInfoMessage(":hbForm:message", "Salvo com sucesso", "");
        } catch (Exception e) {
            addErrorMessage(":hbForm:message", "Não foi possível salvar", "");
        }
    }

    public void generateAnamnese() {
        List<HumanHbMeasure> anemics = new ArrayList<>();
        for (HumanHbMeasure aux : humanHbMeasures) {
            if (aux.hasHb1() && aux.getHb1().getAnemic()) {
                 anemics.add(aux);
            }
        }
        if (anemics.isEmpty()) {
            addErrorMessage(null, "Sêm anêmicos registrados", "Mas poxa vida :/");
        } else {
            try {
                anemnesePDF.run(unity.getOrganization(), anemics);
            } catch (Exception ex) {
                Logger.getLogger(HbBean.class.getName()).log(Level.SEVERE, null, ex);
                addErrorMessage(null, "Mas poxa vida :/", "Contate o administrador do sistema");
            }
        }
    }

    public void generatePrescription() {
        List<Human> humans = new ArrayList<>();
        for (HumanHbMeasure aux : humanHbMeasures) {
            if (aux.hasHb1() && aux.getHb1().getAnemic()) {
                humans.add(aux.getHuman());
            }
        }
        prescriptionPDF.generate(getFacesContext(), unity, humans, concentration, consultationDate);
    }

    public boolean activeUnity() {
        return (this.campaign == null);
    }

    public boolean activeBottom() {
        return ((humanHbMeasures.isEmpty()) || (this.campaign == null) || (this.unity == null));
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public Campaign getCampaign() {
        return campaign;
    }

    public void setCampaign(Campaign campaign) {
        this.campaign = campaign;
    }

    public List<HumanHbMeasure> getHumanHbMeasures() {
        return humanHbMeasures;
    }

    public void setHumanHbMeasures(List<HumanHbMeasure> humanHbMeasures) {
        this.humanHbMeasures = humanHbMeasures;
    }

    public Integer getConcentration() {
        return concentration;
    }

    public void setConcentration(Integer concentration) {
        this.concentration = concentration;
    }

    public Date getConsultationDate() {
        return consultationDate;
    }

    public void setConsultationDate(Date consultationDate) {
        this.consultationDate = consultationDate;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}
